package com.borqs.music.util;

import com.borqs.music.IMediaPlaybackService;
import com.borqs.music.MediaPlaybackService;
import com.borqs.music.R;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.widget.Toast;

public class StopMusicReceiver extends BroadcastReceiver {
    private static final MyLogger logger = MyLogger.getLogger(StopMusicReceiver.class.getSimpleName());

    public static final String ACTION_INIT_STOP_PLAY = "com.borqs.musi.INIT_STOP_PLAY";
    public static final String ACTION_STOP_PLAY = "com.borqs.music.STOP_PLAY";
    public static final String ACTION_CANCEL_STOP_PLAY = "com.borqs.music.CANCEL_STOP_PLAY";
    public static final String EXTRA_LASTED_TIME = "lastedTime";
    public static final String KEY_CANCEL_STOP = "cancel_stop_play";

    private static final int STOP_PLAY_STATUS = 2;

    private NotificationManager mNManager;

    private static Handler mHandler;
    private static Notification notification;

    @Override
    public void onReceive(final Context ctx, Intent intent) {
        logger.d("intent : " + intent);
        mNManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        String action = intent.getAction();

        if (action == null) {
            return;
        }

        if (action.equals(ACTION_INIT_STOP_PLAY)) {
            int lastedTime = intent.getIntExtra(EXTRA_LASTED_TIME, 0);
            long stopMusicTime = System.currentTimeMillis() + (lastedTime * 60000);
            AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, stopMusicTime, getStopSender(ctx, ACTION_STOP_PLAY));

            if (mHandler == null) {
                notification = new Notification(R.drawable.stat_notify_musicplayer,
                        ctx.getString(R.string.statue_content_stop_music, lastedTime),
                        System.currentTimeMillis());
                notification.flags = Notification.FLAG_ONGOING_EVENT;
                notification.setLatestEventInfo(ctx, "", "",getStopSender(ctx, ACTION_CANCEL_STOP_PLAY));
                mHandler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        String statueContent = ctx.getString(R.string.statue_content_stop_music, msg.arg1);
                        notification.contentView.setTextViewText(android.R.id.title, statueContent);
                        mNManager.notify(STOP_PLAY_STATUS, notification);

                        Message msg1 = mHandler.obtainMessage();
                        msg1.arg1 = msg.arg1 - 1;
                        mHandler.sendMessageDelayed(msg1, 60000);
                    }
                };
            }

            Message msg = mHandler.obtainMessage();
            msg.arg1 = lastedTime;
            mHandler.sendMessage(msg);
        } else if (action.equals(ACTION_STOP_PLAY)) {
            IBinder binder = peekService(ctx, new Intent(ctx, MediaPlaybackService.class));
            IMediaPlaybackService sService = IMediaPlaybackService.Stub.asInterface(binder);
            mNManager.cancel(STOP_PLAY_STATUS);
            mHandler.removeCallbacksAndMessages(null);

            try {
                if (sService.isPlaying()) {
                    sService.pause();
                    Toast.makeText(ctx, ctx.getString(R.string.toast_stop_music),
                            Toast.LENGTH_SHORT).show();
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        } else if (action.equals(ACTION_CANCEL_STOP_PLAY)) {
            mNManager.cancel(STOP_PLAY_STATUS);
            mHandler.removeCallbacksAndMessages(null);

            AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
            am.cancel(getStopSender(ctx, ACTION_STOP_PLAY));

            Toast.makeText(ctx, ctx.getString(R.string.toast_cancel_stop_music), Toast.LENGTH_SHORT).show();
        }
    }

    private PendingIntent getStopSender(Context context, String action) {
        Intent stopMusicIntent = new Intent(action);
        PendingIntent stopSender = PendingIntent.getBroadcast(context, 0,
                stopMusicIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        return stopSender;
    }
}
